﻿using Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client.Control
{
    /// <summary>
    /// Pagination.xaml 的交互逻辑
    /// </summary>
    public partial class Pagination : UserControl
    {
        public Pagination()
        {
            InitializeComponent();
            DataContext = this;
        }
        //上一页
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            PaginModel_.CurrentPage = Convert.ToInt16(PaginModel_.CurrentPage) -1;
            NextPageCommand?.Execute(new
            {
                CurrentPage= PaginModel_.CurrentPage,
                SelectedData= SelectedData
            });
        }
        //下一页
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            PaginModel_.CurrentPage = PaginModel_.CurrentPage+1;
            NextPageCommand?.Execute(new
            {
                CurrentPage = PaginModel_.CurrentPage,
                SelectedData = SelectedData
            });
        }
        //public ICommand LastpageCommand
        //{
        //    get { return (ICommand)GetValue(LastpageCommandProperty); }
        //    set { SetValue(LastpageCommandProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for LastpageCommand.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty LastpageCommandProperty =
        //    DependencyProperty.Register("LastpageCommand", typeof(ICommand), typeof(Pagination), new PropertyMetadata(null));


        //
        public ICommand NextPageCommand
        {
            get { return (ICommand)GetValue(NextPageCommandProperty); }
            set { SetValue(NextPageCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NextPageCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NextPageCommandProperty =
            DependencyProperty.Register("NextPageCommand", typeof(ICommand), typeof(Pagination), new PropertyMetadata(new PropertyChangedCallback(NextPageCommand_)));

        private static void NextPageCommand_(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            
        }

        //public ICommand EnterCommand
        //{
        //    get { return (ICommand)GetValue(EnterCommandProperty); }
        //    set { SetValue(EnterCommandProperty, value); }
        //}

        // Using a DependencyProperty as the backing store for NextPageCommand.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty EnterCommandProperty =
        //    DependencyProperty.Register("EnterCommand", typeof(ICommand), typeof(Pagination), new PropertyMetadata(null));
        /// <summary>
        /// 总数据条数
        /// </summary>
        //public int Total
        //{
        //    get { return (int)GetValue(TotalProperty); }
        //    set { SetValue(TotalProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for Total.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty TotalProperty =
        //    DependencyProperty.Register("Total", typeof(int), typeof(Pagination), new PropertyMetadata(new PropertyChangedCallback(Total_)));

        private static void Total_(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //TotalPage = Convert.ToInt16(e.NewValue)/ Pagination.SelectedData;
        }

        private string Navigation_="0";
        /// <summary>
        /// 需要前往的页数
        /// </summary>
        public string Navigation
        {
            get { return Navigation_; }
            set { Navigation_ = value; }
        }
        /// <summary>
        /// 第几页
        /// </summary>
        //public string CurrentPage
        //{
        //    get { return (string)GetValue(CurrentPageProperty); }
        //    set { SetValue(CurrentPageProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for CurrentPage.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty CurrentPageProperty =
        //    DependencyProperty.Register("CurrentPage", typeof(string), typeof(Pagination), new PropertyMetadata("1"));


        /// <summary>
        /// 总页数
        /// </summary>
        //public int TotalPage
        //{
        //    get { return (int)GetValue(TotalPageProperty); }
        //    set { SetValue(TotalPageProperty,value); }
        //}

        //// Using a DependencyProperty as the backing store for TotalPage.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty TotalPageProperty =
        //    DependencyProperty.Register("TotalPage", typeof(int), typeof(Pagination), new PropertyMetadata(0));


        private Dictionary<int,int> PageSize_;

        public Dictionary<int, int> PageSize
        {
            get {
                if(PageSize_ == null|| PageSize_.Any())
                {
                    PageSize_ = new Dictionary<int, int>();
                    PageSize_.Add(10,10);
                    PageSize_.Add(30, 30);
                    PageSize_.Add(60, 60);
                    PageSize_.Add(120, 120);
                }
                return PageSize_; 
            }
            set { PageSize_ = value; }
        }

        private int SelectedData_=10;
        /// <summary>
        /// 选择页大小
        /// </summary>
        public int SelectedData
        {
            get { return SelectedData_; }
            set { SelectedData_ = value; }
        }
        /// <summary>
        /// 前往指定页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                PaginModel_.CurrentPage = int.Parse(Navigation);
                NextPageCommand?.Execute(new
                {
                    CurrentPage = Navigation,
                    SelectedData = SelectedData
                });
            }
        }
        /// <summary>
        /// 最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (PaginModel_.Total > 0 && SelectedData != 0)
            {
                NextPageCommand?.Execute(new
                {
                    CurrentPage = PaginModel_.TotalPage,
                    SelectedData = SelectedData
                });
            }
        
        }



        public PaginModel PaginModel_
        {
            get { return (PaginModel)GetValue(PaginModel_Property); }
            set { SetValue(PaginModel_Property, value); }
        }

        // Using a DependencyProperty as the backing store for PaginModel_.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PaginModel_Property =
            DependencyProperty.Register("PaginModel_", typeof(PaginModel), typeof(Pagination), new PropertyMetadata(new PaginModel()));


    }
}
